package com.zzzy.doc.controller.exchange;

import com.zzzy.common.util.FileUtil;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.common.vo.PageResult;
import com.zzzy.doc.dao.entity.DocReceiveRecord;
import com.zzzy.doc.dao.entity.Document;
import com.zzzy.doc.service.DocService;
import com.zzzy.doc.service.exchange.DocReceiveRecordService;
import com.zzzy.doc.vo.document.DocBasicInfoRes;
import com.zzzy.doc.vo.document.DocInfoRes;
import com.zzzy.doc.vo.document.exchange.receive.DocExchangeReceivePageReq;
import com.zzzy.doc.vo.document.exchange.receive.DocExchangeReceiveQueryRes;
import com.zzzy.doc.vo.pageoffice.OpenOfficeFileRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Tag(name = "公文交换-单位收文", description = "doc201")
@Validated
@RestController
@RequestMapping("/doc/exchange/receive")
public class DocExchangeReceiveController {

    @Resource
    private DocReceiveRecordService docReceiveRecordService;
    @Resource
    private DocService docService;

    @Operation(summary = "单位收文分页列表")
    @GetMapping(value = "/page")
    @PreAuthorize("@pvs.hasPermission('doc:exchange:receive:page')")
    public AjaxResult<PageResult<DocExchangeReceiveQueryRes>> page(@Valid @ParameterObject DocExchangeReceivePageReq queryReq) {
        PageResult<DocExchangeReceiveQueryRes> docReceivePage = docReceiveRecordService.getPage(queryReq);
        return AjaxResult.success(docReceivePage);
    }

    @GetMapping("/getRecord/{id}")
    @Operation(summary = "获取收文记录信息")
    @Parameter(name = "id", description = "收文记录ID", required = true)
    @PreAuthorize("@pvs.hasAnyPermission('doc:exchange:receive:page')")
    public AjaxResult<DocExchangeReceiveQueryRes> getRecord(@NotNull(message = "收文记录ID不能为空") @PathVariable("id") Long id) {
        DocExchangeReceiveQueryRes receiveRecord = docReceiveRecordService.getReceiveByReceiveId(id);
        return AjaxResult.success(receiveRecord);
    }

    @GetMapping("/getDocBasicInfo/{id}")
    @Operation(summary = "获取发文记录中公文基本信息")
    @Parameter(name = "id", description = "收文记录ID", required = true)
    @PreAuthorize("@pvs.hasAnyPermission('doc:exchange:receive:page')")
    public AjaxResult<DocBasicInfoRes> getDocBasicInfo(@NotNull(message = "收文记录ID不能为空") @PathVariable("id") Long id) {
        DocBasicInfoRes doc = docReceiveRecordService.getDocBasicInfo(id);
        return AjaxResult.success(doc);
    }

    @GetMapping("/getDocImage/{id}")
    @Operation(summary = "获取公文正文第一页缩略图")
    @Parameter(name = "id", description = "收文记录ID", required = true)
    @PreAuthorize("@pvs.hasAnyPermission('doc:exchange:receive:page')")
    public void getDocImage(@NotNull(message = "收文记录ID不能为空") @PathVariable("id") Long id, HttpServletResponse response) {
        DocReceiveRecord receiveRecord = docReceiveRecordService.getByIdForQuery(id);
        Document doc = docService.getDocById(receiveRecord.getDocId());
        FileUtil.outputFileStream(response, doc.getDocThumbnailBlob(), doc.getSubject());
    }


    @GetMapping(value = "/getFinalPageOfficeFile/{id}")
    @Operation(summary = "打开最终文件（合并后的pdf）")
    @Parameter(name = "id", description = "收文记录ID", required = true)
    @PreAuthorize("@pvs.hasAnyPermission('doc:exchange:receive:page')")
    public AjaxResult<OpenOfficeFileRes> getFinalPageOfficeFile(@NotNull(message = "收文记录ID不能为空") @PathVariable("id") Long id) {
        String openFileUrl = "/doc/exchange/receive/getFinalDocFileStream/" + id;
        OpenOfficeFileRes openOfficeFileRes = docReceiveRecordService.getFinalPageOfficeResByDocId(id, openFileUrl);
        return AjaxResult.success(openOfficeFileRes);
    }

    @GetMapping(value = "/getFinalDocFileStream/{id}")
    @Operation(summary = "获取最终文件流（合并后的pdf）")
    @Parameter(name = "id", description = "收文记录ID", required = true)
    @PreAuthorize("@pvs.hasAnyPermission('doc:exchange:receive:page')")
    public void getFinalDocFileStream(@NotNull(message = "收文记录ID不能为空") @PathVariable("id") Long id, HttpServletResponse response) {
        DocInfoRes docInfo = docReceiveRecordService.getDocInfo(id);
        byte[] fileBlob = docInfo.getPdfFileBlob();
        FileUtil.outputFileStream(response, fileBlob, docInfo.getSubject() + "." + FileUtil.PDF);
    }

    @GetMapping("/isConfirmReceive/{id}")
    @Operation(summary = "是否有签收公文权限")
    @Parameter(name = "id", description = "收文记录ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:exchange:receive:page')")
    public AjaxResult<Boolean> isConfirmReceive(@NotNull(message = "收文记录ID") @PathVariable("id") Long id) {
        return AjaxResult.success(docReceiveRecordService.isConfirmReceive(id));
    }

    @PutMapping("/confirmReceive/{id}")
    @Operation(summary = "签收公文")
    @Parameter(name = "id", description = "收文记录ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:exchange:receive:page')")
    public AjaxResult confirmReceive(@NotNull(message = "收文记录ID") @PathVariable("id") Long id) {
        docReceiveRecordService.confirmReceive(id);
        return AjaxResult.success();
    }

}
